<template>
  <div class="app-container">
    <div class="custom_card_0" v-show="showSearch">
      <el-form :model="queryParams" ref="queryForm" :inline="true" size="small">
        <el-form-item label1 label="车牌筛选" prop="carNo" label-width="70px">
          <el-input v-model="queryParams.carNo" placeholder="车牌筛选" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label1 label="设备号筛选" prop="deviceNo" label-width="90px">
          <el-input v-model="queryParams.deviceNo" placeholder="设备号筛选" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="custom_card">
      <el-row :gutter="10" class="mb10">
        <el-col :span="1.5">
          <el-button size="medium" class="first-plain-btn" @click="handleAdd"
            v-hasPermi="['car:information:add']">新&nbsp;&nbsp;增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button class="sec-plain-btn" size="medium" :loading="exportLoading" @click="handleExport"
            v-hasPermi="['car:information:export']">导&nbsp;&nbsp;出</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button class="thr-plain-btn" size="medium" v-hasPermi="['car:alarm:list']"
            @click="alarmStatistics">告警统计</el-button>
        </el-col>
        <!--        <el-col :span="1.5">-->
        <!--          <el-button-->
        <!--            class="primary-plain-btn"-->
        <!--            size="medium"-->
        <!--            v-hasPermi="['car:alarm:list']"-->
        <!--            @click="synchronizeMayDayDevices"-->
        <!--          >同步51GPS平台设备</el-button-->
        <!--          >-->
        <!--        </el-col>-->
        <!--        <el-col :span="1.5">-->
        <!--          <el-button-->
        <!--            class="primary-plain-btn"-->
        <!--            size="medium"-->
        <!--            v-hasPermi="['car:alarm:list']"-->
        <!--            @click="synchronizeDiXinDevices"-->
        <!--          >同步帝馨GPS平台设备</el-button-->
        <!--          >-->
        <!--        </el-col>-->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="informationList" @selection-change="handleSelectionChange" border>
        <el-table-column label="设备编号" align="center" prop="deviceNo" min-width="140" :show-overflow-tooltip="true" />
        <el-table-column label="设备供应商" align="center" prop="manufacturer" min-width="120" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.manufacturer" :value="scope.row.manufacturer" />
          </template>
        </el-table-column>
        <el-table-column label="SIM卡号" align="center" prop="simCard" min-width="140" :show-overflow-tooltip="true" />
        <el-table-column label="设备型号" align="center" prop="equipmentModel" min-width="140"
          :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <dict-tag v-if="scope.row.manufacturer == 2" :options="dict.type.equipmentModel"
              :value="scope.row.equipmentModel" />
            <span v-else>/</span>
          </template>
        </el-table-column>
        <el-table-column label="设备类型" align="center" prop="deviceKind" min-width="100" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            {{ scope.row.manufacturer == 2 ? ({ 1: '有线', 2: '无线' }[Number(scope.row.deviceKind)]) : "/" }}
          </template>
        </el-table-column>
        <el-table-column label="车牌号" align="center" prop="carNo" min-width="100" :show-overflow-tooltip="true" />
        <el-table-column label="所属门店" align="center" prop="deptName" min-width="120" :show-overflow-tooltip="true" />
        <el-table-column label="所属租户" align="center" prop="tenantName" min-width="120" :show-overflow-tooltip="true" />
        <!--        <el-table-column-->
        <!--          label="是否启用"-->
        <!--          align="center"-->
        <!--          prop="isAvailable"-->
        <!--          min-width="100"-->
        <!--          :show-overflow-tooltip="true"-->
        <!--        >-->
        <!--          <template slot-scope="scope">-->
        <!--            <dict-tag-->
        <!--              :options="dict.type.sys_available"-->
        <!--              :value="scope.row.isAvailable"-->
        <!--            />-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="操作" align="center" fixed="left" class-name="small-padding fixed-width" width="220">
          <template slot-scope="scope">
            <!--            <el-button-->
            <!--              size="small"-->
            <!--              type="text"-->
            <!--              @click="handleAvailable(scope.row, 1)"-->
            <!--              v-if="scope.row.isAvailable == 0"-->
            <!--              v-hasPermi="['car:information:edit']"-->
            <!--              >启用</el-button-->
            <!--            >-->
            <!--            <el-button-->
            <!--              size="small"-->
            <!--              type="text"-->
            <!--              @click="handleAvailable(scope.row, 0)"-->
            <!--              v-if="scope.row.isAvailable == 1"-->
            <!--              v-hasPermi="['car:information:edit']"-->
            <!--              >停用</el-button-->
            <!--            >-->
            <el-button size="small" type="text" @click="handleBind(scope.row)" v-if="!scope.row.carNo"
              v-hasPermi="['car:information:edit']">绑定</el-button>
            <el-button size="small" type="text" @click="handleUnBind(scope.row)" v-if="scope.row.carNo"
              v-hasPermi="['car:information:edit']">解绑</el-button>
            <el-button size="small" type="text" @click="handleUpdate(scope.row)"
              v-hasPermi="['car:information:edit']">修改</el-button>
            <el-button size="small" type="text" @click="handleDelete(scope.row)"
              v-hasPermi="['car:information:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改设备信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body :destroy-on-close="true">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="归属门店" prop="deptId">
              <el-select v-model="form.deptId" placeholder="请选择归属门店" filterable remote>
                <el-option v-for="item in storeList" :key="item.deptId" :label="item.deptName"
                  :value="parseInt(item.deptId)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="供应商" prop="manufacturer">
              <el-select v-model="form.manufacturer" placeholder="请输入设备供应商" style="width: 100%"
                @change='manufacturerChange'>
                <el-option v-for="item in dict.type.manufacturer" :key="item.value" :label="item.label"
                  :value="parseInt(item.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="设备编号" prop="deviceNo">
              <el-input v-model="form.deviceNo" placeholder="请输入设备编号" />
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="isCarKey">
            <el-form-item label="SIM卡号" prop="simCard">
              <el-input v-model="form.simCard" placeholder="请输入SIM卡号" />
            </el-form-item>
          </el-col>

          <el-col :span="24" v-if="form.manufacturer == 2 && isCarKey">
            <el-form-item label="设备型号" prop="equipmentModel">
              <el-select v-model="form.equipmentModel" style="width: 100%" placeholder="请输入设备型号">
                <el-option v-for="item in dict.type.equipmentModel" :key="item.value" :label="item.label"
                  :value="parseInt(item.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="设备类型" prop="deviceKind" v-if="isCarKey">
              <el-radio v-model.number="form.deviceKind" :label="1">有线</el-radio>
              <el-radio v-model.number="form.deviceKind" :label="2">无线</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="submitloading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择车辆 -->
    <MmvSelectPermissionCar v-model="showxcheliang" @success="submitcheliang" :deptId="deptId" v-if="showxcheliang">
    </MmvSelectPermissionCar>
  </div>
</template>

<script>
import {
  stopUsingEnabled,
  bindingVehicle,
  unbundlingVehicle,
  listInformation,
  getInformation,
  delInformation,
  addInformation,
  updateInformation,
  exportInformation,
  synchronizeMayDayDevices,
  synchronizeDiXinDevices
} from '@/api/car/information'
import MmvSelectPermissionCar from '@/views/components/selectPermissionCar.vue'
import { getDeptList, getStoreList } from "@/api/car/model";

export default {
  name: 'Information',
  components: {
    MmvSelectPermissionCar,
  },
  dicts: ['sys_available', 'manufacturer', 'equipmentModel'],
  data() {
    return {
      isCarKey: true,
      showxcheliang: false,
      deptId: null,
      // 遮罩层
      loading: true,
      submitloading: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备信息表格数据
      informationList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      storeList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceNo: null,
        manufacturer: null,
        simCard: null,
        equipmentModel: null,
        deviceKind: null,
        isAvailable: null,
        deptId: null,
        tenantId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceNo: [{ required: true, message: '请输入设备编号' }],
        manufacturer: [{ required: true, message: '请选择设备供应商' }],
        equipmentModel: [{ required: true, message: '请选择设备型号' }],
        deptId: [{ required: true, message: '请选择门店' }],
        simCard: [{ required: true, message: '请输入SIM卡号' }],
        deviceKind: [{ required: true, message: '' }],
        // deviceNo: [{ required: true, message: '请输入设备编号' }],
        // manufacturer: [{ required: true, message: '请输入SIM卡号' }],
        // equipmentModel: [{ required: true, message: '请输入设备供应商' }],
        // simCard: [{ required: true, message: '请输入设备型号' }],
        // deviceKind: [{ required: true, message: '请输入SIM卡号' }],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    manufacturerChange(event) {
      if (event == 5) {
        this.isCarKey = false
      } else {
        this.isCarKey = true
      }
      console.log(event, 'manufacturerChange')
    },
    submitcheliang(data) {
      // this.form.carId = data.carId;
      // this.form.carNo = data.carNo;
      // this.form.vinNo = data.vinNo;
      // this.form.carInfo = data.carInfo;
      bindingVehicle({
        deviceId: this.form.deviceId,
        carId: data.carId,
        carNo: data.carNo,
      }).then((res) => {
        this.$message.success(res.msg)
        this.showxcheliang = false
        this.getList()
      })
    },
    /** 查询设备信息列表 */
    getList() {
      this.loading = true
      listInformation(this.queryParams).then((response) => {
        this.informationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.isCarKey = true
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        deviceId: null,
        deviceNo: null,
        manufacturer: null,
        simCard: null,
        equipmentModel: null,
        deviceKind: 1,
        delFlag: null,
        isAvailable: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: null,
        tenantId: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.deviceId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.isCarKey = true
      this.title = '添加设备信息'
      this.form.manufacturer = 2
      getDeptList().then((response) => {
        this.storeList = response.data
      })
    },
    alarmStatistics() {
      this.$router.push({ path: '/riskManagement/warn' });
    },
    synchronizeMayDayDevices() {
      this.$modal.loading("同步中...")
      synchronizeMayDayDevices().then((response) => {
        this.getList()
        this.$message.success(response.msg)
      }).finally(err => {
        this.$modal.closeLoading()
      })
    },
    synchronizeDiXinDevices() {
      this.$modal.loading("同步中...")
      synchronizeDiXinDevices().then((response) => {
        this.getList()
        this.$message.success(response.msg)
      }).finally(err => {
        this.$modal.closeLoading()
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

      this.reset()
      const deviceId = row.deviceId
      getDeptList().then((response) => {
        this.storeList = response.data
      })
      getInformation(deviceId).then((response) => {
        this.form = response.data
        if (response.data.manufacturer == 5) {
          this.isCarKey = false
        } else {
          this.isCarKey = true
        }
        console.log(this.isCarKey, 'this.isCarKeythis.isCarKey')
        this.open = true
        this.title = '修改设备信息'
      })
    },
    /** 绑定按钮操作 */
    handleBind(row) {
      this.reset()
      const deviceId = row.deviceId
      getInformation(deviceId).then((response) => {
        this.form = response.data
        this.showxcheliang = true
        this.deptId = this.form.deptId;
      })
    },
    /** 解绑按钮操作 */
    handleUnBind(row) {
      const deviceId = row.deviceId
      unbundlingVehicle(deviceId).then((response) => {
        this.getList()
        this.showxcheliang = false
        this.$message.success(response.msg)
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.submitloading = true
          if (this.form.deviceId != null) {
            updateInformation(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
              this.isCarKey = true
            })
          } else {
            addInformation(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
              this.isCarKey = true
            })
          }
          setTimeout(() => {
            this.submitloading = false
          }, 2e3)
        }
      })
    },
    /** 启用/停用 */
    handleAvailable(row, available) {
      stopUsingEnabled({
        deviceId: row.deviceId,
        isAvailable: available,
      }).then((response) => {
        this.$modal.msgSuccess('修改成功')
        this.open = false
        this.getList()
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceIds = row.deviceId || this.ids
      this.$modal
        .confirm('是否确认删除数据项？')
        .then(function () {
          return delInformation(deviceIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal
        .confirm('是否确认导出所有设备信息数据项？')
        .then(() => {
          this.exportLoading = true
          return exportInformation(queryParams)
        })
        .then((response) => {
          this.$download.name(response.msg)
          this.exportLoading = false
        })
        .catch(() => { })
    },
  },
}
</script>
